package com.jiangxiaofei.citycloud.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.UserInfoBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.MainConstant;
import com.jiangxiaofei.citycloud.mvp.MainPersenter;
import com.jiangxiaofei.citycloud.param.UserInfoParam;
import com.jiangxiaofei.citycloud.util.MD5Util;
import com.jiangxiaofei.citycloud.util.SharedPreferencesUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by JiangXFei on 2017/8/30
 * 用户登录页面
 */

public class LoginAcitivity extends BaseActivity implements MainConstant.UserLoginView {
    @InjectView(R.id.login_img_ioc)
    ImageView loginImgIoc;
    @InjectView(R.id.tv_welcome)
    TextView tvWelcome;
    @InjectView(R.id.tv_regit)
    TextView tvRegit;
    @InjectView(R.id.login_et_phone_number)
    EditText loginEtPhoneNumber;
    @InjectView(R.id.login_et_password)
    EditText loginEtPassword;
    @InjectView(R.id.login_btn_login)
    Button loginBtnLogin;
    @InjectView(R.id.login_tv_forget_password)
    TextView loginTvForgetPassword;
    private String userName;
    private String password;
    MainConstant.UserLoginPersenter userInfoPersenter;
    private UserInfoBean userInfo;

    @Override
    protected void initView(Bundle savedInstanceState) {
        mActivity = this;
        mContext = this;
        setContentView(R.layout.activity_login_layout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ButterKnife.inject(this);
        userInfoPersenter = new MainPersenter(mContext, (MainConstant.UserLoginView) mActivity);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @OnClick({R.id.tv_regit, R.id.login_tv_forget_password, R.id.login_btn_login})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.tv_regit:
                //用户注册
                startActivity(new Intent(mContext, RegistActivity.class));
                break;
            case R.id.login_tv_forget_password:
                //忘记密码
                break;
            case R.id.login_btn_login:
                //用户登陆
                if (isFromOk()) {
                    userInfoPersenter.onLoginUser();
                } else {
                    //登陆失败
                }
                break;
            default:
                break;
        }
    }

    private boolean isFromOk() {
        userName = loginEtPhoneNumber.getText().toString();
        password = loginEtPassword.getText().toString();
        if (EmptyUtils.isEmpty(userName)) {
            Toast.makeText(mContext, "请输入用户名", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (EmptyUtils.isEmpty(password)) {
            Toast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }


    @Override
    public UserInfoParam getUserLoginParam() {

        return new UserInfoParam(userName, MD5Util.MD5Salt2(password));
    }

    @Override
    public void onLoginSuccess(Object o) {
        List<UserInfoBean> beanList = (List<UserInfoBean>) o;
        if (beanList.size() > 0) {
            userInfo = beanList.get(0);
            SharedPreferencesUtil.put(mContext, StringConstants.NAME, userName);
            SharedPreferencesUtil.put(mContext, StringConstants.PASSWORD, password);
            SharedPreferencesUtil.setPrefBoolean(mContext, StringConstants.ISLOGIN, true);
            Constants.userInfo = userInfo;
            Constants.userId = Integer.valueOf(userInfo.getId());
            Toast.makeText(mContext, "登陆成功", Toast.LENGTH_SHORT).show();
            mActivity.finish();
        }
    }
}
